#include "Game/Tilemaps/TerrainTilemap.h"


namespace TD_Game {
	
	
	/************************************************************************/
	/* Constructors                                                         */
	/************************************************************************/
	TerrainTilemap::TerrainTilemap() : Tilemap() {

	}
	TerrainTilemap::TerrainTilemap(Tileset *g_pTileset) : Tilemap(g_pTileset) {

	}

	bool TerrainTilemap::loadTilemap(const Tileset *g_pTileset, const string &tileMapStr, const Coord2D & mapPosition, const Coord2D &tileSize, const Coord2D &numTiles) {
		if(g_pTileset == NULL) {
			return false;
		}
		m_pTileset = g_pTileset;
		return(loadTilemap(tileMapStr, mapPosition, tileSize, numTiles));
	}

	bool TerrainTilemap::loadTilemap(const string &tileMapStr, const Coord2D & mapPosition, const Coord2D &tileSize, const Coord2D &numTiles) {

		m_numTiles = numTiles;
		m_tileSize = tileSize;
		m_position = mapPosition;

		if(!loadTileMatrix(tileMapStr)) {
			return false;
		}
		
		for(unsigned int i = 0; i < m_numTiles.y; i++) {
			for(unsigned int j = 0; j < m_numTiles.x; j++) {
				if((*m_pTilemapMatrix)(i, j) == 1) {
					/************************************************************************/
					/* 0,0 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineTop(i, j) || (*m_pTilemapMatrix)(i - 1, j) == 0) { //First element: 0
						if(borderlineLeft(i, j) || (*m_pTilemapMatrix)(i, j - 1) == 0) { //0, 0
							m_tileVector.push_back(createNewTile(i, j, 0, 0, Tileset::topLeft));
						} else { //0, 1
							m_tileVector.push_back(createNewTile(i, j, 0, 0, Tileset::topMiddle));
						}
					} else { //First element: 1
						if(borderlineLeft(i, j) || (*m_pTilemapMatrix)(i, j - 1) == 0) { //1, 0
							m_tileVector.push_back(createNewTile(i, j, 0, 0, Tileset::middleLeft));
						} else { //1, 1 it is a corner
							m_tileVector.push_back(createNewTile(i, j, 0, 0, Tileset::middleMiddle + 3));
						}
					}
					/************************************************************************/
					/* 1,0 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineTop(i, j) || (*m_pTilemapMatrix)(i - 1, j) == 0) { 
						m_tileVector.push_back(createNewTile(i, j, 1, 0, Tileset::topMiddle));
					} else {
						m_tileVector.push_back(createNewTile(i, j, 1, 0, Tileset::middleMiddle));
					}

					/************************************************************************/
					/* 2,0 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineTop(i, j) || (*m_pTilemapMatrix)(i - 1, j) == 0) { //First element: 0
						if(borderlineRight(i, j) || (*m_pTilemapMatrix)(i, j + 1) == 0) { //0, 0
							m_tileVector.push_back(createNewTile(i, j, 2, 0, Tileset::topRight));
						} else { //0, 1
							m_tileVector.push_back(createNewTile(i, j, 2, 0, Tileset::topMiddle));
						}
					} else { //First element: 1
						if(borderlineRight(i, j) || (*m_pTilemapMatrix)(i, j + 1) == 0) { //1, 0
							m_tileVector.push_back(createNewTile(i, j, 2, 0, Tileset::middleRight));
						} else { //1, 1 it is a corner
							m_tileVector.push_back(createNewTile(i, j, 2, 0, Tileset::middleMiddle + 2));
						}
					}

					/************************************************************************/
					/* 0, 1 ELEMENT                                                         */
					/************************************************************************/
					if(borderlineLeft(i, j) || (*m_pTilemapMatrix)(i, j - 1) == 0) { 
						m_tileVector.push_back(createNewTile(i, j, 0, 1, Tileset::middleLeft));
					} else {
						m_tileVector.push_back(createNewTile(i, j, 0, 1, Tileset::middleMiddle));
					}

					/************************************************************************/
					/* 1, 1 ELEMENT                                                         */
					/************************************************************************/
					m_tileVector.push_back(createNewTile(i, j, 1, 1, Tileset::middleMiddle));

					/************************************************************************/
					/* 2, 1 ELEMENT                                                         */
					/************************************************************************/
					if(borderlineRight(i, j) || (*m_pTilemapMatrix)(i, j + 1) == 0) { 
						m_tileVector.push_back(createNewTile(i, j, 2, 1, Tileset::middleRight));
					} else {
						m_tileVector.push_back(createNewTile(i, j, 2, 1, Tileset::middleMiddle));
					}

					/************************************************************************/
					/* 0,2 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineBottom(i, j) || (*m_pTilemapMatrix)(i + 1, j) == 0) { //First element: 0
						if(borderlineLeft(i, j) || (*m_pTilemapMatrix)(i, j - 1) == 0) { //0, 0
							m_tileVector.push_back(createNewTile(i, j, 0, 2, Tileset::bottomLeft));
						} else { //0, 1
							m_tileVector.push_back(createNewTile(i, j, 0, 2, Tileset::bottomMiddle));
						}
					} else { //First element: 1
						if(borderlineLeft(i, j) || (*m_pTilemapMatrix)(i, j - 1) == 0) { //1, 0
							m_tileVector.push_back(createNewTile(i, j, 0, 2, Tileset::middleLeft));
						} else { //1, 1 it is a corner
							m_tileVector.push_back(createNewTile(i, j, 0, 2, Tileset::topRight + 2));
						}
					}

					/************************************************************************/
					/* 1,2 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineBottom(i, j) || (*m_pTilemapMatrix)(i + 1, j) == 0) { 
						m_tileVector.push_back(createNewTile(i, j, 1, 2, Tileset::bottomMiddle));
					} else {
						m_tileVector.push_back(createNewTile(i, j, 1, 2, Tileset::middleMiddle));
					}
					/************************************************************************/
					/* 2,2 ELEMENT                                                          */
					/************************************************************************/
					if(borderlineBottom(i, j) || (*m_pTilemapMatrix)(i + 1, j) == 0) { //First element: 0
						if(borderlineRight(i, j) || (*m_pTilemapMatrix)(i, j + 1) == 0) { //0, 0
							m_tileVector.push_back(createNewTile(i, j, 2, 2, Tileset::bottomRight));
						} else { //0, 1
							m_tileVector.push_back(createNewTile(i, j, 2, 2, Tileset::bottomMiddle));
						}
					} else { //First element: 1
						if(borderlineRight(i, j) || (*m_pTilemapMatrix)(i, j + 1) == 0) { //1, 0
							m_tileVector.push_back(createNewTile(i, j, 2, 2, Tileset::middleRight));
						} else { //1, 1 it is a corner
							m_tileVector.push_back(createNewTile(i, j, 2, 2, Tileset::topRight + 1));
						}
					}
					
				} else {
					m_tileVector.push_back(createNewTile(i, j, 0, 0, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 0, 1, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 0, 2, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 1, 0, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 1, 1, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 1, 2, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 2, 0, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 2, 1, Tileset::bottomRight + 2));
					m_tileVector.push_back(createNewTile(i, j, 2, 2, Tileset::bottomRight + 2));
				}
			}
		}
				

		return true;
	}

}